<template>
  <el-dialog v-bind="{ ...$attrs }" class="app-tag" >
    <el-form ref="ruleFormRef" :model="ruleForm" label-width="120px"  status-icon>
        <el-form-item label="标签" prop="title">
          <el-input v-model="ruleForm.title" @blur="hangleInputBlur" />
          <el-tag v-for="(item,index) in tags" closable :key="index" type="primary" @close="handleTagClose(index)">{{item}}</el-tag> 
        </el-form-item>
      </el-form>
      <template #footer>
          <el-button  @click="$emit('close')"> 关闭 </el-button>
          <el-button  type="primary" @click="submitForm(ruleFormRef)"> 确认 </el-button>
      </template>
      <Builder title="UTM Builder" v-model="builderDialog" @close="builderDialog=false" @success="getLineTarget"></Builder>
  </el-dialog>
</template>
 
<script lang="ts" setup>
import { reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage, ElMessageBox } from 'element-plus'
import { setTags } from '@/server/home'
const emit = defineEmits<{(event: 'close'): void;(event: 'success'): void;  }>()
const props = defineProps({
    id:{
      type:String,
      default:()=>''
    },
    list:{
      type:Array,
      default:()=>[]
    }
})
//定义属性
interface RuleForm {
  title: string
}
//开关
const builderDialog = ref(false)
//表单
const formSize = ref('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
  title: ''
})


//tag标签
const tags=reactive(props.list)
const hangleInputBlur=() => {
  ruleForm.title &&  tags.push(ruleForm.title)
  ruleForm.title=''
}
//关闭tag标签
const handleTagClose=(index:number)=>{
  tags.splice(index,1)
}
//提交
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      try{
        await setTags(props.id,{tags})
        ElMessage.success("修改成功")
        emit("success")
      } catch(err){
        ElMessage.error("修改失败")
      }
      
    } else {
      console.log('error submit!', fields)
    }
  })
}


</script>
<style lang="scss" scoped>
.app-tag{
  .el-tag{
    margin-top: 10px;
    margin-right: 10px;
  }
}
</style>